/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Member;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DatabaseUtil;

/**
 *
 * @author zhang
 */
public class MemberDao {
    public int addMember(String name,String gender,String phone,String email,String password,String profession,String liketype){
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("insert into member(name,gender,phone,email,password,profession,liketype) values(?,?,?,?,?,?,?)");
            ps.setString(1, name);
            ps.setString(2, gender);
            ps.setString(3,phone);
            ps.setString(4,email);
            ps.setString(5,password);
            ps.setString(6,profession);
            ps.setString(7,liketype);
            i=ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return i;
    }
    
    public Member getMemberByName(String name){
        Member member=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from member where name=?");
            ps.setString(1,name);
            rs=ps.executeQuery();
            while(rs.next()){
                member=new Member(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getFloat(9),rs.getFloat(10),rs.getBoolean(11));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return member;
    }
    
    public List<Member> getMemberByKeyword(String keyword){
        List<Member> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from member where name like ?");
            ps.setString(1,"%"+keyword+"%");
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new Member(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getFloat(9),rs.getFloat(10),rs.getBoolean(11)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    
    public List<Member> getAllMember(){
        List<Member> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from member");
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new Member(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getFloat(9),rs.getFloat(10),rs.getBoolean(11)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    
    public Member getMemberById(int id){      //根据ID找到用户
        Member member=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from member where id=?");
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while(rs.next()){
                member=new Member(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getFloat(9),rs.getFloat(10),rs.getBoolean(11));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return member;
    }
    
    public String getMemberNameByPhone(String phone){
        String name=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from member where phone=? or email=?");
            ps.setString(1,phone);
            ps.setString(2,phone);
            rs=ps.executeQuery();
            while(rs.next()){
                name=rs.getString(2);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return name;
    }
    
    
    public Boolean Login(String phone,String password){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from member where ((phone= ? and password= ?) or (email= ? and password= ?)) and isforbid=0");
            ps.setString(1,phone);
            ps.setString(2,password);
            ps.setString(3,phone);
            ps.setString(4,password);
            rs=ps.executeQuery();
            if(rs.next()){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return false;
    }
    
    public Boolean forbidUser(int id){    //禁用某个用户
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("update member set isforbid=1 where id=?");
            ps.setInt(1,id);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
    
    public Boolean openUser(int id){    //解禁用户
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("update member set isforbid=0 where id=?");
            ps.setInt(1,id);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
}
